package com.meitianhui.convenitenbuy.widget;

import android.animation.ValueAnimator;
import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.LinearInterpolator;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.meitianhui.convenitenbuy.R;
import com.meitianhui.convenitenbuy.util.GlideUtils;


public class FlopView extends FrameLayout {

    private Context mContext;
    public static final int ROTATION_X = 0;//绕X轴旋转
    public static final int ROTATION_Y = 1;//绕Y轴旋转
    public static final int ROTATION_Z = 2;//绕Z轴旋转
    public float[] mAnimArgsOne = new float[]{0, 90, 180};//动画的动态参数
    public float[] mAnimArgsTwo = new float[]{180, 270, 360};//动画的动态参数
    public int mDuration = 1000;//动画时长:默认1000毫秒
    public int mRunningCount;//已经成功执行动画的次数

    private int mAnimatorType = ROTATION_Y;//默认为竖直方法旋转


    private ValueAnimator mAnimator;
    private ValueAnimator mAnimatorOne;
    private ValueAnimator mAnimatorTwo;

    public FlopView(Context context) {
        super(context);
        mContext = context;
        init();
    }


    public FlopView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        init();
    }

    public FlopView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        init();
    }


    private void init() {
        initAnimation();//初始化动画

        //给自身设置点击事件
        setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mRunningCount == 0) {

                    if (flowviewListener != null) {
                        flowviewListener.onListener();
                    }
                }
            }
        });

    }

    public FlopviewOnListener flowviewListener;

    public interface FlopviewOnListener {
         void onListener();

    }

    public void setonFlopviewListener(FlopviewOnListener flowviewListener) {
        this.flowviewListener = flowviewListener;
    }

    /**
     * 设置旋转动画的类型 分X轴，Y轴，Z轴
     */
    public void setAnimatorType(int rotationType) {
        if (mAnimator != null) {
            mAnimator.cancel();
        }
        if (mAnimArgsOne != null) {
            mAnimatorOne.cancel();
        }
        if (mAnimatorTwo != null) {
            mAnimatorTwo.cancel();
        }
        mRunningCount = 0;
        mAnimatorType = rotationType;
    }

    /**
     * 设置动画时长
     *
     * @param duration 时长
     */
    public void setmDuration(int duration) {
        mDuration = duration;
    }

/*    */

    /**
     * 设置参数
     *//*
    public void setArgs(float... args){
        if (args!=null){
            for (int i = 0; i <args.length ; i++) {
                mAnimArgsOne[i]=args[i];
            }
        }
    }*/


    //根据当前动画的状态判断是否开启动画
    public void startAnimation() {
        if (mRunningCount == 0) {
            if (mRunningCount % 2 == 0) {
                mAnimator = mAnimatorOne;
            } else {
                mAnimator = mAnimatorTwo;
            }
            if (mAnimator == null)
                return;
            if (mAnimatorOne.isRunning() || mAnimatorTwo.isRunning()) {
                Toast.makeText(mContext, "开奖进行中", Toast.LENGTH_SHORT).show();
                return;
            }
 /*       mIsRunning = mAnimator.isRunning();
        if (mIsRunning) {
            Toast.makeText(mContext,"还没结束，不理你0.0",Toast.LENGTH_SHORT).show();
            return;
        }*/
            mRunningCount++;
            mAnimator.start();
        }
    }

    /**
     * 设置动画的前景色和背景色
     *
     * @param foreSrcId 前景
     * @param backSrcId 背景
     */
    public void setForeAndBackImage(int foreSrcId, int backSrcId, String url) {
        this.removeAllViews();
        if (mAnimator != null) {
            mAnimator.cancel();
        }
        if (mAnimArgsOne != null) {
            mAnimatorOne.cancel();
        }
        if (mAnimatorTwo != null) {
            mAnimatorTwo.cancel();
        }
        mRunningCount = 0;

        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        View layout1 = LayoutInflater.from(mContext).inflate(R.layout.ggl_item_flopview, null);
        ImageView foreImage = (ImageView) layout1.findViewById(R.id.ggl_item_flopview);
        foreImage.setImageResource(foreSrcId);


        View layout2 = LayoutInflater.from(mContext).inflate(R.layout.ggl_item_flopview, null);
        ImageView backImage = (ImageView) layout2.findViewById(R.id.ggl_item_flopview);
        if (TextUtils.isEmpty(url)) {
            backImage.setImageResource(backSrcId);
        } else {
            GlideUtils.setRectImage(mContext,url, R.mipmap.icon_default_app, R.mipmap.icon_default_app, backImage);
        }
        addView(layout2, 0, params);
        addView(layout1, 1, params);
    }

    /**
     * 设置动画的前景 和 背景要显示的View
     *
     * @param foreView 前景View
     * @param backView 背景View
     */
    public void setForeAndBackView(View foreView, View backView) {
        this.removeAllViews();
        if (foreView.getParent() != null) {
            ((LinearLayout) foreView.getParent()).removeView(foreView);
        }
        if (backView.getParent() != null) {
            ((LinearLayout) backView.getParent()).removeView(backView);
        }

        if (mAnimator != null) {
            mAnimator.cancel();
        }
        if (mAnimArgsOne != null) {
            mAnimatorOne.cancel();
        }
        if (mAnimatorTwo != null) {
            mAnimatorTwo.cancel();
        }
        mRunningCount = 0;

        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        addView(backView, 0, params);
        addView(foreView, 1, params);
    }

    /**
     * 初始化动画
     */
    private void initAnimation() {
        if (mAnimatorOne == null) {
            mAnimatorOne = ValueAnimator.ofFloat(mAnimArgsOne);
            mAnimatorOne.setDuration(mDuration);
            mAnimatorOne.setInterpolator(new LinearInterpolator());//匀速的插值器
            mAnimatorOne.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    float value = (float) animation.getAnimatedValue();
                    if (getChildCount() > 0 && getChildCount() == 2) {
//                        如果有2个孩子，让其执行动画
                        if (mAnimatorType == ROTATION_X) {
                            getChildAt(0).setRotationX(value);
                            getChildAt(1).setRotationX(value);
                        } else if (mAnimatorType == ROTATION_Y) {
                            getChildAt(0).setRotationY(value);
                            getChildAt(1).setRotationY(value);
                        }
                        if (value >= 0 && value <= 90) {
                            if (mRunningCount % 2 == 0) {
                                getChildAt(1).setVisibility(View.INVISIBLE);
                                getChildAt(0).setVisibility(View.VISIBLE);
                            } else {
                                getChildAt(1).setVisibility(View.VISIBLE);
                                getChildAt(0).setVisibility(View.INVISIBLE);
                            }
                        } else if (value > 90 && value <= 180) {
                            if (mRunningCount % 2 == 0) {
                                getChildAt(1).setVisibility(View.VISIBLE);
                                getChildAt(0).setVisibility(View.INVISIBLE);
                            } else {
                                getChildAt(1).setVisibility(View.INVISIBLE);
                                getChildAt(0).setVisibility(View.VISIBLE);
                            }
                        }
                    }
                }
            });
        }

        if (mAnimatorTwo == null) {
            mAnimatorTwo = ValueAnimator.ofFloat(mAnimArgsTwo);
            mAnimatorTwo.setDuration(mDuration);
            mAnimatorTwo.setInterpolator(new LinearInterpolator());//匀速的插值器
            mAnimatorTwo.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    float value = (float) animation.getAnimatedValue();
                    if (getChildCount() > 0 && getChildCount() == 2) {
                        //如果有2个孩子，让其执行动画
                        if (mAnimatorType == ROTATION_X) {
                            getChildAt(0).setRotationX(value);
                            getChildAt(1).setRotationX(value);
                        } else if (mAnimatorType == ROTATION_Y) {
                            getChildAt(0).setRotationY(value);
                            getChildAt(1).setRotationY(value);
                        }
                        if (value >= 180 && value <= 270) {
                            if (mRunningCount % 2 == 0) {
                                getChildAt(1).setVisibility(View.INVISIBLE);
                                getChildAt(0).setVisibility(View.VISIBLE);
                            } else {
                                getChildAt(1).setVisibility(View.VISIBLE);
                                getChildAt(0).setVisibility(View.INVISIBLE);
                            }
                        } else if (value > 270 && value <= 360) {
                            if (mRunningCount % 2 == 0) {
                                getChildAt(1).setVisibility(View.VISIBLE);
                                getChildAt(0).setVisibility(View.INVISIBLE);
                            } else {
                                getChildAt(1).setVisibility(View.INVISIBLE);
                                getChildAt(0).setVisibility(View.VISIBLE);
                            }
                        }
                    }
                }
            });
        }


    }

}